import APIURL from '../../APIURL';

/**
 * 音乐播放器数据
 */
const state = {
  musicStatus: {
    connect: false,
    status: false,
    loading: false,
    playing: false,
  },
  id: 0,
  type: '',
  title: '',
  singer: '',
  singerHeadpic: '',
  musicSrc: '',
  volume: Number(localStorage.getItem('volume')) || 100,
  muted: JSON.parse(localStorage.getItem('muted') || false),
  randomStatus: JSON.parse(localStorage.getItem('randomStatus') || false),
  musicInfo: JSON.parse(localStorage.getItem('musicInfo') || '{}'),
  musicList: JSON.parse(localStorage.getItem('musicList') || '[]'),
};

const getters = {

};

const mutations = {
  play(state) {
    state.musicStatus.playing = true;
  },
  pause(state) {
    state.musicStatus.playing = false;
  },
  changeInfo(state, info) {
    for (const key in state) {
      if (typeof (info[key]) !== 'undefined') {
        state[key] = info[key];
      }
    }
    localStorage.setItem('musicInfo', JSON.stringify(state.musicInfo));
  },
  changeVolume(state, volume) {
    state.volume = volume;
    localStorage.setItem('volume', volume);
  },
  setMute(state) {
    state.muted = true;
    localStorage.setItem('muted', true);
  },
  setUnMute(state) {
    state.muted = false;
    localStorage.setItem('muted', false);
  },
  setRandom(state) {
    state.randomStatus = true;
    localStorage.setItem('randomStatus', false);
  },
  setUnRandom(state) {
    state.randomStatus = false;
    localStorage.setItem('randomStatus', false);
  },
  addMusicList(state, list) {
    if (Array.isArray(list)) {
      state.musicList.push(...list);
    } else {
      state.musicList.push(list);
    }
    localStorage.setItem('musicList', JSON.stringify(state.musicList));
  },
  removeMusicList(state, index) {
    state.musicList.splice(index, 1);
    localStorage.setItem('musicList', JSON.stringify(state.musicList));
  },
  changeMusicList(state, list) {
    state.musicList.length = 0;
    for (let i = 0; i < list.length; i += 1) {
      state.musicList.push(list[i]);
    }
    localStorage.setItem('musicList', JSON.stringify(state.musicList));
  },
  changeMusicStatus(state, musicStatus) {
    for (const key in musicStatus) {
      if (typeof (state.musicStatus[key]) !== 'undefined') {
        state.musicStatus[key] = musicStatus[key];
      }
    }
  },
};

const actions = {
  getMusic({
    commit,
    dispatch,
  }, musicInfo) {
    commit('changeMusicStatus', {
      loading: true,
      connect: false,
      status: false,
    });
    return new Promise((resolve, reject) => {
      APIURL.SONGINFO(musicInfo.id, musicInfo.type)
        .then((res) => {
          const data = res.data;
          commit('changeMusicStatus', {
            loading: false,
            connect: true,
            status: data.success,
          });
          if (data.success) {
            const info = data.data;
            dispatch('playNewMusic', info)
              .then(() => {
                resolve();
              })
              .catch(() => {
                reject();
              });
            // commit('changeInfo', {
            //   id: info.ID,
            //   type: info.SK,
            //   title: info.SN,
            //   singer: info.S,
            //   singerHeadpic: info.user.I,
            //   musicSrc: info.KL,
            //   musicInfo: info,
            // });
            // resolve();
          } else {
            reject();
          }
        })
        .catch(() => {
          commit('changeMusicStatus', {
            load: false,
            connect: false,
            status: false,
          });
          reject();
        });
    });
  },
  playNewMusic({
    commit,
  }, musicInfo) {
    return new Promise((resolve) => {
      commit('changeInfo', {
        id: musicInfo.ID,
        type: musicInfo.SK,
        title: musicInfo.SN,
        singer: musicInfo.S,
        singerHeadpic: musicInfo.user.I,
        musicSrc: musicInfo.KL,
        musicInfo,
      });
      commit('play');
      resolve();
    });
  },
  initPlayer({
    commit,
    state,
  }) {
    return new Promise((resolve) => {
      if (state.musicInfo) {
        commit('changeInfo', {
          id: state.musicInfo.ID,
          type: state.musicInfo.SK,
          title: state.musicInfo.SN,
          singer: state.musicInfo.S,
          singerHeadpic: state.musicInfo.user.I,
          musicSrc: state.musicInfo.KL,
        });
      }
      resolve();
    });
  },
};

export default {
  state,
  getters,
  mutations,
  actions,
};
